import { unzip } from "./zip.js";
enum methodEnum {
  "GET" = "GET",
  "OPTIONS" = "OPTIONS",
  "HEAD" = "HEAD",
  "POST" = "POST",
  "PUT" = "PUT",
  "DELETE" = "DELETE",
  "TRACE" = "TRACE",
  "CONNECT" = "CONNECT",
}
interface resultInter {
  url: string;
  method?: methodEnum;
  headers?: any;
  params?: any;
  data?: any;
}
const baseUrl = "https://biquge.hope-api.top/biquge/api/";

const request = ({ url, method, header = {}, data = {} }: resultInter) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: baseUrl + url,
      method: method || "GET",
      responseType: "text",
      header: {
        "Content-Type": "application/json;charset=utf-8", // 设置请求头的编码为 UTF-8
        Credential: "biquge.hope-api.top", // 设置请求头的编码为 UTF-8
        ...header,
      },
      data,
      dataType: "json",
      success: (res) => {
        const body = unzip(res.data.data);
        resolve(body);
      },
      fail: (error) => {
        console.log("bug:", error);
        return reject(error);
      },
    });
  });
};
export default request;
